//
//  GoodStoryViewController.m
//  NBTripe
//
//  Created by Lee on 16/1/9.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "GoodStoryViewController.h"
#import "GoodStoysCollectionViewCell.h"
#import "GoodStorysDetailViewController.h"
#import "RecommendSecondModel.h"
#import "UserModel.h"
@interface GoodStoryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionview;

@property (nonatomic, strong) NSMutableArray *storysArray;

@end

@implementation GoodStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
  
    // Do any additional setup after loading the view.
}
/**
 *  获取数据
 */
- (void)getData {
    self.storysArray = [[NSMutableArray alloc] initWithCapacity:0];
   [XYNetWorkTool getWithUrl:kAllButoonURL parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
       NSDictionary *dic = result;
       NSArray *stoyarray = [[dic objectForKey:@"data"] objectForKey:@"hot_spot_list"];
       for (NSDictionary *dic in stoyarray) {
           RecommendSecondModel *model =[[RecommendSecondModel alloc] initWithDataSource:dic];
           [self.storysArray addObject:model];
       }
         [self createCollectionview];
   } fail:nil];
}
/**
 *  创建collectionview
 */
- (void)createCollectionview {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake(WIDTH/2-5, 200*XHIGHT);
    flowLayout.minimumLineSpacing = 10*XHIGHT;
    self.collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT-108) collectionViewLayout:flowLayout];
    //scrollview关闭适配
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionview.dataSource = self;
    self.collectionview.delegate = self;
    self.collectionview.bounces = NO;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    [self.collectionview registerNib:[UINib nibWithNibName:@"GoodStoysCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"GoodStoysCollectionViewCellIdentifier"];
    self.collectionview.pagingEnabled = YES;
    [self.view addSubview:self.collectionview];
}
/**
 *  CollectionView的协议方法
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.storysArray.count;
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //NSLog(@"%@", self.storysArray);
    GoodStoysCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoodStoysCollectionViewCellIdentifier" forIndexPath:indexPath];
    cell.model = self.storysArray[indexPath.row];
    cell.usermodel = [self.storysArray[indexPath.row] usermodel];
    return cell;
}
/**
 *  colletionview点击事件
 */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodStorysDetailViewController *detailVC = [[GoodStorysDetailViewController alloc] init];
    detailVC.spotId = [self.storysArray[indexPath.row] spot_id];
    detailVC.usermodel = [self.storysArray[indexPath.row] usermodel];
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
